<?php

namespace app\api\controller\clockin;
use app\api\service\clockin\ClockinActivityPlanTaskService;
use app\api\service\clockin\ClockinActivityService;
use app\api\service\log\AdminOperateLogService;
use app\api\service\log\UserOperateLogService;
use app\BaseController;
use app\api\service\clockin\ClockinActivityPlanService;
use app\util\AdminUserUtil;
use app\util\CommonUtil;
use app\api\service\develop\DatagridColumnService;
use app\util\DateUtil;
use app\util\ResponseUtil;
use app\util\UserUtil;
use Cassandra\Date;

class ClockinActivityPlan extends BaseController
{
    public function getClockinActivityPlanInfo()
    {
        $id = $this->requestData['id'];
        $service = new ClockinActivityPlanService();
        $finalData = $service->findRowById($id);
        return ResponseUtil::success($finalData);
    }

    public function getClockinActivityPlanList()
    {
        try{

            $pageIndex = $this->requestData['page'];
            $pageSize = $this->requestData['rows'];
            if(empty($this->requestData['clockin_activity_id'])){
                ResponseUtil::failure("请指定打卡活动");
            }
            $clockinActivityId = $this->requestData['clockin_activity_id'];
            $serviceClockinActivity = new ClockinActivityService();
            $activityInfo = $serviceClockinActivity->findRowById($clockinActivityId);
            if($activityInfo){
                ResponseUtil::failure("打卡活动不存在");
            }
            $service = new ClockinActivityPlanService();
            $this->requestData['content_type'] = $activityInfo['content_type'];
            $list = $service->getListWithPage($this->requestData, $pageIndex, $pageSize);
            $total = $service->getTotalNum($this->requestData);
            $finalData = CommonUtil::getPaginationData($list, $total);
            return ResponseUtil::success($finalData);
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("查询失败");
        }
    }

    public function getClockinActivityPlanOption()
    {
        $service = new ClockinActivityPlanService();
        $list = $service->getListNoPage($this->requestData);
        return ResponseUtil::success($list);
    }

    /**
     * 2024-09-21 同一活动，已经加入的诗词不可再次加入
     * @return \think\response\Json
     */
    public function addClockinActivityPlan()
    {
        try{
            if(empty($this->requestData['clockin_activity_id'])){
                return ResponseUtil::failure("请指定打卡活动");
            }
            if(empty($this->requestData['plan_date'])){
                return ResponseUtil::failure("请指定打卡时间");
            }
            if(empty($this->requestData['arr_subject_id'])){
                return ResponseUtil::failure("请指定打卡内容");
            }
            $clockinActivityId = $this->requestData['clockin_activity_id'];
            $arrSubjectId = $this->requestData['arr_subject_id'];
            $planDate  = $this->requestData['plan_date'];
            $serviceClockinActivity = new ClockinActivityService();
            $activityInfo = $serviceClockinActivity->findRowById($clockinActivityId);
            if(empty($activityInfo)){
                return ResponseUtil::failure("打卡活动不存在");
            }

            $serviceClockinActivityPlan = new ClockinActivityPlanService();
            $planInfo = $serviceClockinActivityPlan->findRowByData(['clockin_activity_id' => $clockinActivityId, 'plan_date' => $planDate, 'status'=>1]);
            if($planInfo && $planInfo['plan_date'] != $planDate){
                return ResponseUtil::failure("计划时间已使用");
            }

            $serviceClockinActivityPlanTask = new ClockinActivityPlanTaskService();
            $condition = array(
                'content_type' => $activityInfo['content_type'],
                'clockin_activity_id' => $clockinActivityId,
                'arr_subject_id' => $arrSubjectId,
                'status'=>1
            );
            $taskList = $serviceClockinActivityPlanTask->getListNoPage($condition);
            if($taskList){
                return ResponseUtil::failure("不可重复加入活动");
            }

            $arrFieldTitle = array('activity_name' => '打卡活动', 'plan_date' => '打卡计划时间');

            $service = new ClockinActivityPlanService();
            $res = $service->addNewClockinActivityPlan($clockinActivityId, $planDate, $arrSubjectId);
            if($res){
                $this->requestData['activity_name'] = $activityInfo['activity_name'];
                $addInfo = CommonUtil::getDataInsertMessage($this->requestData, $arrFieldTitle);
                $message = '新增打卡活动计划';
                if($addInfo){
                    $message .= ','.$addInfo;
                }
                $logData = [
                    'subject_id' => $res,
                    'remark'  => '',
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('ClockinActivityPlan', AdminOperateLogService::ARR_ACTION_TYPE['add'], $logData);
                return ResponseUtil::success($res);
            }else{
                return ResponseUtil::failure("新增打卡活动计划失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("新增打卡活动计划失败");
        }
    }

    public function deleteMultiClockinActivityPlan(){
        try{
            $id = $this->requestData['id'];
            if(!is_array($id)){
                return ResponseUtil::failure("参数格式有误");
            }
            $service = new ClockinActivityPlanService();
            $deleteData = [];
            foreach($id as $value){
                $info = $service->getClockinActivityPlanInfoById($value);
                if(!$info){
                    return ResponseUtil::failure("数据不存在");
                }
                $deleteData[] = $info;
            }
            $res = $service->deleteClockinActivityPlanByManyId($id);
            if($res){
                $deleteInfo = CommonUtil::getDataDeleteMessage($deleteData, ['##field_1##', '##field_2##']);
                $messageCn = '删除任务项,'.$deleteInfo;
                $messageEn = '';
                $logData = [
                    'subject_id' => json_encode($id),
                    'remark'  => implode(',', array_column($deleteData, '##field_1##')),
                    'description_en' => $messageEn,
                    'description_cn' => $messageCn,
                ];
                UserUtil::addOperateLog('ClockinActivityPlan', UserOperateLogService::ARR_ACTION_TYPE['delete'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("删除失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("删除失败");
        }
    }

    public function getClockinActivityPlanListByMonth()
    {
        try{
            if(empty($this->requestData['clockin_activity_id'])){
                return ResponseUtil::failure("请指定要查看的活动");
            }
            $clockinActivityId = $this->requestData['clockin_activity_id'];

            if(!empty($this->requestData['plan_month'])){
                $planMonth = DateUtil::dealMonthDate($this->requestData['plan_month']);
            }else{
                $planMonth = DateUtil::getCurrentDatetime("Y-m");
            }
            $service = new ClockinActivityPlanService();
            $finalData = $service->getListByMonth($clockinActivityId, $planMonth);
            return ResponseUtil::success($finalData);
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("查询失败");
        }
    }

    public function addMultiClockinActivityPlan()
    {
        try{
            if(empty($this->requestData['clockin_activity_id'])){
                return ResponseUtil::failure("请指定打卡活动");
            }
            if(empty($this->requestData['plan_date'])){
                return ResponseUtil::failure("请指定打卡时间");
            }
            if(empty($this->requestData['subject_id'])){
                return ResponseUtil::failure("请指定打卡内容");
            }
            if(!is_array($this->requestData['subject_id'])){
                return ResponseUtil::failure("参数格式有误");
            }

            $clockinActivityId = $this->requestData['clockin_activity_id'];
            $arrSubjectId = $this->requestData['subject_id'];
            $planDate  = $this->requestData['plan_date'];
            $serviceClockinActivity = new ClockinActivityService();
            $activityInfo = $serviceClockinActivity->findRowById($clockinActivityId);
            if(empty($activityInfo)){
                return ResponseUtil::failure("打卡活动不存在");
            }

            $contentType = $serviceClockinActivity::ARR_CONTENT_TYPE['movie_word'];
            $serviceClockinActivityPlan = new ClockinActivityPlanService();
            $addData = array();
            foreach($arrSubjectId as $subjectId){
                $addData[] = array(
                    'clockin_activity_id' => $clockinActivityId,
                    'content_type'     => $contentType,
                    'plan_date'     => $planDate,
                    'subject_id'    => $subjectId,
                    'status' => 1
                );

            }
            $res = $serviceClockinActivityPlan->createManyRow($addData);
            if(false !== $res){
                if(0 == $res){
                    return ResponseUtil::failure("单词均已添加至计划");
                }
                $message = '新增打卡活动计划';
                $logData = [
                    'subject_id' => $res,
                    'remark'  => json_encode($res),
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('ClockinActivityPlan', AdminOperateLogService::ARR_ACTION_TYPE['add'], $logData);
                return ResponseUtil::success($res);
            }else{
                return ResponseUtil::failure("新增打卡活动计划失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("新增打卡活动计划失败");
        }
    }

}



